﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesFile;

namespace ZJGIS.EngineControl
{
    /// <summary>
    /// 作用：选择测区范围控件
    /// 作者：叶茂
    /// 修改日期：2009-06-19
    /// </summary>
    public partial class ChooseFWX : UserControl
    {
        private IPolygon m_pPolygon;
        private IPolyline m_pPolyline;

        private FormAddData m_frmAddData1;

        private List<esriGeometryType> geometryTypes;

        public IPolygon PolygonFWX
        {
            get
            {
                return m_pPolygon;
            }
        }

        public IPolyline PolylineFWX
        {
            get
            {
                return m_pPolyline;
            }
        }

        public void AddGeometryType(esriGeometryType geometryType)
        {
            if (geometryTypes == null) geometryTypes = new List<esriGeometryType>();
            geometryTypes.Add(geometryType);
        }

        public string LoadBtnText
        {
            set { this.btnLoad.Text = value; }
        }

        public ChooseFWX()
        {
            InitializeComponent();
            geometryTypes = new List<esriGeometryType>();
            //默认就可以加载面层图层
            geometryTypes.Add(esriGeometryType.esriGeometryPolygon);
        }

        /// <summary>
        /// 选择测区图层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmdAddDataOk_Click(object sender, EventArgs e)
        {
            List<IDataset> allDatasets = m_frmAddData1.AddDataControl.GetAllDatasets();

            if (allDatasets.Count <= 0)
                return;

            IDataset dataset = allDatasets[0];
            if (dataset == null)
            {
                MessageBox.Show("请选择正确的数据！");
                m_frmAddData1.Hide();
                return;
            }

            if (dataset.Type == esriDatasetType.esriDTFeatureDataset ||
                dataset.Type == esriDatasetType.esriDTRasterCatalog ||
                dataset.Type == esriDatasetType.esriDTRasterDataset)
            {
                MessageBox.Show("请选择要素类！");
                m_frmAddData1.Hide();
                return;
            }

            IFeatureClass pFeatureClass = dataset as IFeatureClass;
            IFeatureLayer pFeatureLayer = null;
            if (geometryTypes.Contains(pFeatureClass.ShapeType))
            //if (pFeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
            {
                int iCount = pFeatureClass.FeatureCount(null);
                if (iCount == 1)
                {
                    pFeatureLayer = new FeatureLayerClass();
                    pFeatureLayer.Name = pFeatureClass.AliasName;
                    pFeatureLayer.FeatureClass = pFeatureClass;
                }
                else
                {
                    MessageBox.Show("所选数据中必须是仅有一个要素！");
                    m_frmAddData1.Hide();
                    return;
                }
            }
            else
            {
                MessageBox.Show("所选数据的几何图形不符合要求！");
                m_frmAddData1.Hide();
                return;
            }

            if (pFeatureLayer != null)
            {
                //测区范围面预览
                this.axMapControl1.ClearLayers();
                this.axMapControl1.AddLayer((ILayer)pFeatureLayer);
                this.axMapControl1.get_Layer(0).Visible = true;

                //获取测区范围面
                IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
                IFeature pFeature = pFeatureCursor.NextFeature();

                switch (pFeatureClass.ShapeType)
                {
                    case esriGeometryType.esriGeometryPolyline:
                        m_pPolyline = (IPolyline)pFeature.ShapeCopy;
                        break;
                    case esriGeometryType.esriGeometryPolygon:
                        m_pPolygon = (IPolygon)pFeature.ShapeCopy;
                        ITopologicalOperator2 pTop = m_pPolygon as ITopologicalOperator2;
                        pTop.IsKnownSimple_2 = false;
                        pTop.Simplify();
                        m_pPolyline = pTop.Boundary as IPolyline;
                        break;
                }
            }
            else
            {
                MessageBox.Show("测区范围面层必须是仅有一个要素的单链要素层");
                m_frmAddData1.Hide();
                return;
            }

            m_frmAddData1.Hide();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (m_frmAddData1 == null)
                m_frmAddData1 = new FormAddData();

            m_frmAddData1.AddDataControl.SetMultiSelect(false);
            m_frmAddData1.AddDataControl.OnOkClick = new EventHandler(CmdAddDataOk_Click);
            m_frmAddData1.ShowDialog(this);
        }
    }
}
